/**
 * 
 */
package com.real.fact.web.sale.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.Validate;

import com.real.fact.web.customer.domain.Payment;

/**
 * Represents the association between a sale and a payment, knowing that a
 * payment can apply to multiple sales and also a sale can be payed through
 * different payments.
 */
@Entity
@Table(name = "sale_payment")
public class SalePayment {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id", nullable = false)
  private long id;

  @ManyToOne
  private Payment payment;

  @ManyToOne
  private Sale sale;

  @Column
  private float amount;

  /**
   * Hibernate constructor.
   */
  protected SalePayment() {
  }

  /**
   * Creates a sale payment.
   * @param thePayment
   * @param theSale
   * @param theAmount
   */
  public SalePayment(final Payment thePayment, final Sale theSale,
      final float theAmount) {
    Validate.notNull(thePayment);
    Validate.notNull(theSale);
    Validate.isTrue(theAmount >= 0);
    payment = thePayment;
    sale = theSale;
    amount = theAmount;
  }

  public Payment getPayment() {
    return payment;
  }

  public Sale getSale() {
    return sale;
  }

  public float getAmount() {
    return amount;
  }

  public long getId() {
    return id;
  }

}
